package com.bocaizp.operation.mapper;

import com.bocaizp.pojo.DTO.ChatRecordDTO;
import com.bocaizp.pojo.VO.AddFriendVO;
import com.bocaizp.pojo.VO.FriendListVO;
import com.bocaizp.pojo.VO.OtherInfoVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Author : huang jie
 * Date : 2024/5/9 23:58
 **/
@Mapper
public interface ChatMapper {

	/**
	 * 查询好友列表
	 * @param id
	 * @return
	 */
	@Select("select user_one_id as ownId, user_two_id as id,end_time as lastTime from dialogue where user_one_id = #{id} or user_two_id = #{id}" +
			" order by end_time desc")
	List<FriendListVO> getuserOne(Long id);

	@Select("select username as name, avatar as logo, company, boss_job from user where id = #{id}")
	FriendListVO getinfo(Long id);

	@Select("select content from chat_records where receiver_id = #{id} and sender_id = #{fId} or receiver_id = #{fId} and sender_id = #{id} order by send_time desc limit 0,1")
	String getContent(@Param("id") Long id,@Param("fId") Long fId);

	@Select("select sender_id as ownId, content as text,status from chat_records where sender_id = #{ownId} and receiver_id = #{id} or sender_id = #{id} and receiver_id = #{ownId} " +
			"order by send_time ")
	List<ChatRecordDTO> getHistory(@Param("ownId") Long ownId,@Param("id") String id);

	@Select("select id,username,avatar,company,boss_job from user where id = #{id}")
	OtherInfoVO getotherInfo(String id);

	/**
	 * 插入聊天消息
	 *
	 * @param sid
	 * @param othrtId
	 * @param mess
	 * @param res
	 * @return
	 */
	@Insert("insert into chat_records (sender_id, receiver_id, content, status) values " +
			"(#{sid},#{othrtId},#{mess},#{res})")
	void insert(@Param("sid") Long sid,@Param("othrtId") Long othrtId, @Param("mess") String mess,@Param("res") int res);

	/**
	 * 搜索好友
	 * @param username
	 * @return
	 */
	@Select("select id,username,avatar,create_time from user where username like concat('%',#{username},'%')")
	List<AddFriendVO> addFriendList(String username);

	/**
	 * 添加好友
	 * @param ownId
	 * @param id
	 */
	@Insert("insert into dialogue (user_one_id, user_two_id) VALUES " +
			"(#{ownId},#{id})")
	void build(@Param("ownId") Long ownId,@Param("id") Long id);

	/**
	 * 更新最后一次发消息时间
	 *
	 * @param id
	 * @param rid
	 * @param localDateTime
	 */
	@Update("update dialogue set end_time = #{localDateTime} where user_one_id = #{id} and user_two_id = #{rid} or user_two_id = #{id} and user_one_id = #{rid}")
	void updateTime(@Param("id") Long id,@Param("rid") Long rid,@Param("localDateTime") LocalDateTime localDateTime);

	/**
	 * 删除好友对话框
	 * @param ownId
	 * @param id
	 */
	@Delete("delete from dialogue where user_one_id = #{ownId} and user_two_id = #{id} or user_one_id = #{id} and user_two_id = #{ownId}")
	void delete(@Param("ownId") Long ownId,@Param("id") Long id);

	/**
	 * 删除聊天记录
	 * @param ownId
	 * @param id
	 */
	@Delete("delete from chat_records where sender_id = #{ownId} and receiver_id = #{id} or sender_id = #{id} and receiver_id = #{ownId}")
	void deleteRecords(@Param("ownId") Long ownId,@Param("id") Long id);
}
